#include "PhoneNumber.h"

PhoneNumber::PhoneNumber ()
{
    memset (area_, 0, sizeof area_);
    memset (phone_, 0, sizeof phone_);
    memset (exchange_, 0, sizeof exchange_);
    memset (line_, 0, sizeof line_);
}

ostream& operator<< (ostream& lhs, const PhoneNumber& rhs)
{
    lhs << '(' << rhs.area_ << ") " << rhs.exchange_ 
        << '-' << rhs.line_;

    return lhs;
}

istream& operator>> (istream& lhs, PhoneNumber& rhs)
{
    char buffer[128];
    lhs.getline (buffer, 128);

    if (buffer[0] != '(' || buffer[1] == '0' || buffer[1] == '1'
            || (buffer[7] != '0' && buffer[7] != '1'))
    {
        lhs.clear (ios::failbit);
        cerr << "Invalid phone number entered.";
        return lhs;
    }

    strncpy (rhs.area_, buffer + 1, 3);
    strncpy (rhs.exchange_, buffer + 6, 3);
    strncpy (rhs.line_, buffer + 10, 4);
    strncpy (rhs.phone_, buffer + 1, 15);

    return lhs;
}
